<template>
  <div>
    <el-dialog  title="新增品牌信息" :visible.sync="dialogVisible" v-if="dialogVisible"  :key="timer" width="800px">
      <el-form :inline="true" class="demo-form-inline" label-width="100px">
        <el-form-item label="品牌名称">
          <el-input v-model="data.name" />
        </el-form-item>
         <el-form-item label="链接地址">
          <el-input v-model="data.url" />
        </el-form-item>
        <el-form-item label="LOGO图片">
          <el-upload ref="up1" v-if="dialogVisible" :key="timer"
            class="upload-demo"
            action="/myupload/upload/img?type=brand"
            :on-success="logoSuccess"
            :on-remove="logoRemove"
            :file-list="logoFileList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="品牌精选图片">
          <el-upload ref="up2" v-if="dialogVisible" :key="timer"
            class="upload-demo"
            action="/myupload/upload/img?type=brand"
            :on-success="indexImgSuccess"
            :on-remove="indexImgRemove"
            :file-list="indexImgFileList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="上传BANNER">
          <el-upload ref="up3"
            class="upload-demo"
            action="/myupload/upload/img?type=brand"
            :on-success="bannerSuccess"
            :on-remove="bannerRemove"
            :file-list="bannerFileList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="上传APP品牌BANNER">
          <el-upload ref="up4"
            class="upload-demo"
            action="/myupload/upload/img?type=brand"
            :on-success="appBannerSuccess"
            :on-remove="appBannerRemove"
            :file-list="appBannerFileList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import apibrandManage from "@/api/frontManage/brandManage";
export default {
  name: "add_brand",
  data() {
    return {
      timer:0,
      dialogVisible: false,
      logoFileList: [],
      indexImgFileList: [],
      bannerFileList: [],
      appBannerFileList: [],
      data: {
        name: "",
        url: "", //链接地址
        picturePath: "", //logo图片
        indexImgPath: "", //品牌精选
        bannerPath: "", //pc banner
        appBannerPath: "" //app banner
      }
    };
  },
  methods: {
    open(id) {
      this.timer = new Date().getTime()
      this.logoFileList = [] //新增打开时为空
          this.indexImgFileList = []
          this.bannerFileList = []  
          this.appBannerFileList = []
      this.dialogVisible = true;
      if (id) {
        apibrandManage.get(id).then(res => {
          debugger
          let item = res.data.item;
          this.data = item;
          this.logoFileList = [{ name: "logo", url: item.picturePathHttp }];
          if(item.indexImgPathHttp){
            this.indexImgFileList = [ { name: "品牌精选图片", url: item.indexImgPathHttp } ];
          }
          if(item.bannerPathHttp){
             this.bannerFileList = [{ name: "pc banner", url: item.bannerPathHttp }];
          }
         if(item.appBannerPathHttp){
           this.appBannerFileList = [{ name: "app banner", url: item.appBannerPathHttp }];
         }
        });
      } else {
        this.data = {
          name: "",
          url: "", //链接地址
          picturePath: "", //logo图片
          indexImgPath: "", //品牌精选
          bannerPath: "", //pc banner
          appBannerPath: "" //app banner
        }
      }
    },
    //提交
    submit() {
      debugger
      if (this.data.id) {
        apibrandManage.update(this.data).then(res => {
          this.$parent.getList(); //调父组件方法 刷新表格
          this.dialogVisible = false;
        });
      } else {
        apibrandManage.save(this.data).then(res => {
          this.$parent.getList(); //调父组件方法 刷新表格
          this.dialogVisible = false;
        });
      }
    },
    //logo
    logoSuccess(file) {
      this.data.picturePath = file.imgUrl;
    },
    logoRemove(file) {
      this.data.picturePath = "";
    },

    //品牌精选
    indexImgSuccess(file) {
      this.data.indexImgPath = file.imgUrl;
    },
    indexImgRemove(file) {
      this.data.indexImgPath = "";
    },

    //pc banner
    bannerSuccess(file) {
      this.data.bannerPath = file.imgUrl;
    },
    bannerRemove(file) {
      this.data.bannerPath = "";
    },

    //app banner
    appBannerSuccess(file) {
      this.data.appBannerPath = file.imgUrl;
    },
    appBannerRemove(file) {
      this.data.appBannerPath = "";
    }
  }
};
</script>

<style scoped>
</style>

